1 : <?php
2 : /*--------------------------------------------------------------------------+
3 : This file is part of eStudy.
4 : courses/classes/class.tree.inc.php
5 : - Modulgruppe: Kurse
6 : - Beschreibung: Abstrakte Baumklasse.
7 : - Version: 0.5, 12/05/06
8 : - Autor(en): Christoph Gockel <christoph.gockel@mni.fh-giessen.de>
9 : Jan Marco Müller <jan.m.mueller@mni.fh-giessen.de>
10 : +---------------------------------------------------------------------------+
11 : This program is free software; you can redistribute it and/or
12 : modify it under the terms of the GNU General Public License
13 : as published by the Free Software Foundation; either version 2
14 : of the License, or any later version.
15 : +---------------------------------------------------------------------------+
16 : This program is distributed in the hope that it will be useful,
17 : but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : GNU General Public License for more details.
20 : You should have received a copy of the GNU General Public License
21 : along with this program; if not, write to the Free Software
22 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 : +--------------------------------------------------------------------------*/
24 :
25 : /** Knotenobjekte*/
26 : require_once PATH_TO_ROOT."courses/classes/class.node.inc.php";
27 : /** Blattobjekte*/
28 : require_once PATH_TO_ROOT."courses/classes/class.leaf.inc.php";
29 :
30 : /**
31 : * Die abstrakte Klasse Tree dient dazu die generelle Handhabung mit Baeumen zu
32 : * realisieren.
33 : * @package eStudy.Courses
34 : * @author Christoph Gockel <christoph.gockel@mni.fh-giessen.de>
35 : * @version 0.5, 12/05/06
36 : * @abstract
37 : */
38 : class Tree
39 : {
40 : /**
41 : * Speichert die Struktur des Baumes als (assoziatives) array.
42 : */
43 : var $_tree;
44 : protected $translate;
45 :
46 : /**
47 : * Konstruktor initialisiert den Baum als leeren Baum.
48 : */
49 : function Tree() {
50 0 : $this->translate = getTranslate("courses");
51 0 : $this->_tree = array();
52 0 : }
53 :
54 : public function setTree( &$tree ) {
55 0 : $this->_tree = $tree;
56 0 : }
57 :
58 : public function getTree() {
59 0 : return $this->_tree;
60 : }
61 :
62 : /**
63 : * Erstellt einen neuen Knoten utnerhalb des Elementes mit Id <code>$id</code>.
64 : * Falls kein Element mit Id <code>'$id'</code> gefunden wurde wird auch nichts
65 : * eingefuegt, der Baum bleibt also unveraendert.
66 : * @param $parentId Die Id des Knoten in den das folgende Element eingehaengt
67 : * werden soll.
68 : * @param $node Eine Referenz auf ein Knotenobjekt was eingefuegt wird.
69 : */
70 : function addNode($parentId, &$node) {
71 0 : if ($parentId == 0) { // root Ebene
72 0 : $this->_tree[] =& $node;
73 0 : } else {
74 0 : $tmp =& $this->getNode($parentId, $this->_tree);
75 :
76 0 : if ($tmp != null) {
77 0 : $tmp->setChild($node);
78 0 : } else {
79 : // TODO: bessere Fehlerbehandlung...
80 0 : echo "NULL POINTER! @ ".$node->__toString()."<br />\n";
81 : }
82 : }
83 0 : }
84 :
85 : /**
86 : * Gibt eine Referenz auf den Knoten mit der Nummer $id zurck.
87 : * Falls es das letzte Element was gesucht wird, muss trotzdem erst der gesamte
88 : * Baum durchlaufen werden.
89 : * @param $id Die ID nach der gesucht wird.
90 : * @param $limb Der aktuelle Zweig des Baumes in dem gesucht wird.
91 : * @return mixed <code>null</code> wenn kein Element gefunden wurde, ansonsten
92 : * die Referenz auf das gesuchte Knotenobjekt.
93 : */
94 : function &getNode($id, &$limb = null) {
95 0 : if ( $limb === null ) $limb =& $this->_tree;
96 0 : $res = null; // Rueckgabewert, benoetigt fuer rekursiven Abstieg...
97 :
98 0 : if (is_array($limb) && (count($limb) > 0)) {
99 0 : foreach ($limb as &$branch) {
100 : // und nur weiterschauen wenn sich der aktuelle Knoten auch
101 : // tatsaechlich um einen Knoten handelt
102 0 : if (is_a($branch, 'node') || is_a($branch, 'node_ecom')) {
103 : // stimmt die Id ueberein sind wir fertig
104 0 : if ($branch->getId() == $id) {
105 0 : $res =& $branch;
106 0 : break;
107 : } else { // falls nicht, weiter mit rekursivem Abstieg
108 0 : if (count($branch->getChild()) > 0) {
109 0 : $res =& $this->getNode($id, $branch->getChild());
110 : // Eintrag wurde gefunden, die uebrigen Eintraege
111 : // muessen ignoriert werden, da der gefundene
112 : // Wert sonst wieder ueberschrieben wird.
113 0 : if ( $res !== null ) return $res;
114 0 : }
115 : }
116 0 : }
117 0 : }
118 0 : }
119 :
120 0 : return $res;
121 : }
122 :
123 : /**
124 : * Prueft ob der Baum leer ist.
125 : * @return <code>true</code>/<code>false</code>, je nachdem ob der Baum leer
126 : * ist oder nicht.
127 : */
128 : function isEmpty() {
129 0 : return (count($this->_tree) == 0);
130 : }
131 : }
|